package edu.inf3.j2ee.ejb;


import java.sql.SQLException;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;

import edu.inf3.j2ee.dao.CredentialDao;
import edu.inf3.j2ee.entities.Credentials;


@Stateful(name="SessionManager", mappedName="ejb/SessionManagerJNDI")
public class SessionManagerImpl implements SessionManager {

	private Credentials cred;
	private boolean isAuthenticated;
	
	@PostConstruct
	public void Initialize (){
		this.isAuthenticated = false;
	}
	
	public void Authenticate(String login, String pwd) throws Exception {
			this.cred = CredentialDao.getCredentials(login, pwd);
			if (this.cred != null){
				this.isAuthenticated = true;
			}
			
	}

	public boolean isAuthed() {
		
		return (this.isAuthenticated);
	}

	public boolean isAllowed (String perm, String target){
		return (this.cred.getGroup().permit(perm, target));
	}

}
